#include <iostream>
#include <tuple>
#include "sequential_list.h"

using namespace std;

template <typename T>
SequentialList<T>::SequentialList(int capacity)
{
    this->cap = capacity;
    this->len = 0;
    this->elements = new T[capacity];
}

template <typename T>
void SequentialList<T>::Clear()
{
    this->len = 0;
    delete this->elements;
    this->elements = new T[this->cap];
}

template <typename T>
bool SequentialList<T>::IsEmpty() const
{
    return this->Len() == 0;
}

template <typename T>
bool SequentialList<T>::IsFull() const
{
    return this->Len() == this->cap;
}

template <typename T>
int SequentialList<T>::Len() const
{
    return this->len;
}

template <typename T>
tuple<bool, T> SequentialList<T>::Get(int index) const
{
    T element = T();
    if (index < 0)
    {
        index += this->len;
    }
    if (index < 0 || index >= this->len)
    {
        return make_tuple(false, element);
    }
    return make_tuple(true, this->elements[index]);
}

template <typename T>
int SequentialList<T>::Find(T element) const
{
    for (int i = 0; i < this->cap; i++)
    {
        if (this->elements[i] == element)
        {
            return i;
        }
    }
    return -1;
}

template <typename T>
tuple<bool, T> SequentialList<T>::Prev(T element) const
{
    int index = this->Find(element);
    if (index > 0)
    {
        return make_tuple(true, this->elements[index - 1]);
    }
    return make_tuple(false, T());
}

template <typename T>
tuple<bool, T> SequentialList<T>::Next(T element) const
{
    int index = this->Find(element);
    if (index < this->len - 1)
    {
        return make_tuple(true, this->elements[index + 1]);
    }
    return make_tuple(false, T());
}

template <typename T>
bool SequentialList<T>::Insert(int index, T element)
{
    if (this->IsFull())
    {
        return false;
    }
    if (index < 0)
    {
        index += this->len;
    }
    if (index < 0 || index > this->len)
    {
        return false;
    }
    for (int i = this->len - 1; i >= index; i--)
    {
        this->elements[i + 1] = this->elements[i];
    }
    this->elements[index] = element;
    this->len++;
    return true;
}

template <typename T>
tuple<bool, T> SequentialList<T>::Delete(int index)
{
    if (index < 0)
    {
        index += this->len;
    }
    if (index < 0 || index >= this->len)
    {
        return make_tuple(false, T());
    }
    T element = this->elements[index];
    for (int i = index; i < this->len - 1; i++)
    {
        this->elements[i] = this->elements[i + 1];
    }
    this->len--;
    return make_tuple(true, element);
}

template <typename T>
bool SequentialList<T>::Remove(T element)
{
    int index = this->Find(element);
    if (index == -1)
    {
        return false;
    }
    for (int i = index; i < this->len - 1; i++)
    {
        this->elements[i] = this->elements[i + 1];
    }
    this->len--;
    return true;
}

int main()
{
    // 创建一个容量为10的顺序表
    SequentialList<int> *list = new SequentialList<int>(10);

    // 插入元素并打印
    for (int i = 0; i < 10; i++)
    {
        list->Insert(i, i);
        bool success;
        int element;
        tie(success, element) = list->Get(i);
        cout << element << " ";
    }
    cout << endl;

    // 打印顺序表的长度
    cout << "Length of list: " << list->Len() << endl;

    // 判断顺序表是否为空
    cout << "Is list empty? " << (list->IsEmpty() ? "Yes" : "No") << endl;

    // 判断顺序表是否已满
    cout << "Is list full? " << (list->IsFull() ? "Yes" : "No") << endl;

    // 获取指定位置的元素
    bool success;
    int element;
    tie(success, element) = list->Get(2);
    if (success)
    {
        cout << "Element at index 2: " << element << endl;
    }
    else
    {
        cout << "Failed to get element at index 2" << endl;
    }

    // 查找指定元素的索引
    int index = list->Find(5);
    cout << "Index of element 5: " << index << endl;

    // 获取指定元素的前驱和后继元素
    tie(success, element) = list->Prev(5);
    if (success)
    {
        cout << "Previous element of 5: " << element << endl;
    }
    else
    {
        cout << "5 has no previous element" << endl;
    }

    tie(success, element) = list->Next(5);
    if (success)
    {
        cout << "Next element of 5: " << element << endl;
    }
    else
    {
        cout << "5 has no next element" << endl;
    }

    // 删除指定位置的元素
    tie(success, element) = list->Delete(2);
    if (success)
    {
        cout << "Deleted element at index 2: " << element << endl;
    }
    else
    {
        cout << "Failed to delete element at index 2" << endl;
    }

    // 删除指定元素
    bool removed = list->Remove(7);
    if (removed)
    {
        cout << "Removed element 7" << endl;
    }
    else
    {
        cout << "Failed to remove element 7" << endl;
    }

    // 清空顺序表
    list->Clear();

    return 0;
}